﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WP09S03_OfficeTracking
{
    public partial class FrmReport : WP09S03_OfficeTracking.ParentClass.ParentForm
    {
        public FrmReport()
        {
            InitializeComponent();
        }

        private void lblSubTitle_Click(object sender, EventArgs e)
        {

        }

        private void FrmReport_Load(object sender, EventArgs e)
        {
            this.comboBoxReport.SelectedIndex = 0;
        }



        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            Controller.ReportController rc = new Controller.ReportController();
            FrmReportView f = new FrmReportView();
            
            
            try
            {

                switch (this.comboBoxReport.SelectedIndex)
                {
                    //List of all Employees
                    case 0:
                        {
                            rc.GetAllEmployeesData(f.reportViewer);
                            break;
                        }
                    //List of all Books and Equipments
                    case 1:
                        {
                            rc.GetAllBooksAndEquipments(f.reportViewer);
                            break;
                        }
                    //List of all Office Supplies
                    case 2:
                        {
                            rc.GetAllSupplies(f.reportViewer);
                            break;
                        }
                    //List of all Borrowed Items per Employee
                    case 3:
                        {
                            rc.GetBorrowedBooksAndEquipPerEmployee(f.reportViewer);
                            break;
                        }
                    //List of all Overdue items
                    case 4:
                        {
                            rc.GetOverdueBooksAndEquip(f.reportViewer);
                            break;
                        }

                    //List of Office Supplies to be replenished
                    case 5:
                        {
                            rc.GetSuppliesToBeReplenished(f.reportViewer);
                            break;
                        }

                    //Employees with their Office supplies costs
                    case 6:
                        {
                            rc.GetEmployeesSuppliesCost(f.reportViewer);
                            break;
                        }
                    //Office supplies costs review
                    case 7:
                        {
                            rc.GetMonthlyCost(f.reportViewer);
                            break;
                        }
                    //Transactions History  
                    case 8:
                        {
                            System.Nullable<System.DateTime> usedStartTimeFrom = null;
                            System.Nullable<System.DateTime> usedStartTimeTo = null;
                            System.Nullable<System.DateTime> usedEndTimeFrom = null;
                            System.Nullable<System.DateTime> usedEndTimeTo = null;
                            showDatePanel();
                            if (this.cbIssueDate.Checked)
                            {
                                usedStartTimeFrom = Convert.ToDateTime(this.dtpIssueDateFrom.Text);
                                usedStartTimeTo = Convert.ToDateTime( this.dtpIssueDateTo.Text);
                            }
                            if (this.cbReturnDate.Checked)
                            {
                                usedEndTimeFrom = Convert.ToDateTime(this.dtpReturnDateFrom.Text);
                                usedEndTimeTo = Convert.ToDateTime(this.dtpReturnDateTo.Text);
                            } 

                            switch(this.cbbReportType.SelectedIndex)
                            {
                                case 0:
                                    rc.GetIssueTransaction(f.reportViewer,usedStartTimeFrom, usedStartTimeTo, usedEndTimeFrom, usedEndTimeTo);
                                    break;
                                case 1:
                                    rc.GetReturnTransaction(f.reportViewer, usedStartTimeFrom, usedStartTimeTo);
                                    break;
                                case 2:
                                    rc.GetReplenishTransaction(f.reportViewer, usedStartTimeFrom, usedStartTimeTo);
                                    break;
                            }
                            break;
                        }
                    default:
                        {
                            return;
                            
                        }

                }
                f.Text = this.comboBoxReport.Text;
                
                f.Show();
            }
            catch (Exception exc)
            {
                SetMessage(exc.Message.ToString());
            }
        }

        private void comboBoxReport_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBoxReport.SelectedIndex == 8)
            {
                this.pnlTransactionPara.Visible = true;
            }
            else
            {
                this.pnlTransactionPara.Visible = false;
            }
            this.cbbReportType.SelectedIndex = 0;
            this.cbIssueDate.Checked = false;
            this.cbReturnDate.Checked = false;
            isEnableIssueDate();
            isEnableReturnDate();
        }

        private void cbIssueDate_CheckedChanged(object sender, EventArgs e)
        {
            isEnableIssueDate();
        }

        private void isEnableIssueDate()
        {
            if (this.cbIssueDate.Checked)
            {
                this.dtpIssueDateFrom.Enabled = true;
                this.dtpIssueDateTo.Enabled = true;
            }
            else
            {
                this.dtpIssueDateFrom.Enabled = false;
                this.dtpIssueDateTo.Enabled = false;
            }
        }

        private void cbReturnDate_CheckedChanged(object sender, EventArgs e)
        {
            isEnableReturnDate();
        }

        private void isEnableReturnDate()
        {
            if (this.cbReturnDate.Checked)
            {
                this.dtpReturnDateFrom.Enabled = true;
                this.dtpReturnDateTo.Enabled = true;
            }
            else
            {
                this.dtpReturnDateFrom.Enabled = false;
                this.dtpReturnDateTo.Enabled = false;
            }
        }

        private void cbbReportType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cbIssueDate.Checked = false;
            this.cbReturnDate.Checked = false;
            isEnableIssueDate();
            isEnableReturnDate();
            showDatePanel();
        }

        private void showDatePanel()
        {
            if (this.cbbReportType.SelectedIndex == 0)
            {
                this.pnlIssueDate.Visible = true;
                this.pnlReturnDate.Visible = true;
                this.lblDate.Text = "IssueDate:";
            }
            else
            {
                this.pnlIssueDate.Visible = true;
                this.pnlReturnDate.Visible = false;
                this.lblDate.Text = "CreateDate:";
            }
        }
    }
}
